package com.ecoolex.ying.space.core.manager;

import com.ecoolex.framework.common.util.Check;
import com.ecoolex.ying.space.core.dao.UserLikeAndFavoriteReadStateDAO;
import com.ecoolex.ying.space.entity.UserLikeAndFavoriteReadState;
import com.ecoolex.ying.space.entity.UserLikeAndFavoriteReadState.Column;

import java.time.Instant;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLikeAndFavoriteReadStateManager {
	
    @Autowired
    private UserLikeAndFavoriteReadStateDAO dao;
    
    public UserLikeAndFavoriteReadState get(Integer readUserId) {
    	return dao.selectByPrimaryKeyWithLogicalDelete(readUserId, UserLikeAndFavoriteReadState.NOT_DELETED);
    }
    
    public void updateReceivedUserLikeAndFavoriteReadState(Integer readUserId, String createUser) {
    	UserLikeAndFavoriteReadState readState = get(readUserId);
    	if(Check.notNull(readState)) {
    		readState.setNewReadTime(Date.from(Instant.now()));
    		readState.setUpdateUser(createUser);
    		readState.setUpdateTime(Date.from(Instant.now()));
    		dao.updateByPrimaryKeySelective(readState, Column.newReadTime,Column.updateUser,Column.updateTime);
    	}else {
    		UserLikeAndFavoriteReadState record = new UserLikeAndFavoriteReadState();
    		record.setReadUserId(readUserId);
    		record.setNewReadTime(Date.from(Instant.now()));
    		record.setDeleted(UserLikeAndFavoriteReadState.NOT_DELETED);
    		record.setCreateTime(Date.from(Instant.now()));
    		record.setCreateUser(createUser);
    		record.setUpdateUser(createUser);
    		record.setUpdateTime(Date.from(Instant.now()));
    		dao.insert(record);
    	}
    }
}